package cn.com.callback

import cn.com.elite.EliteBean
import cn.com.elite.Util
import cn.com.kh.getProperty
import groovy.json.JsonSlurper
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.json.JSONObject

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse


/**
 *
 * @Title: aipHSTQWBQtest
 * @Description:华盛证券 企业微信手动拉取 客户标签--内部接口
 * @author: cheih
 * @date 2021-07-06 10:28:00
 *
 */

public class aipHSTQWBQtest implements ServiceBackInvoke {
	private static final Log log = LogFactory.getLog(aipHSTQWBQtest.class);

	public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

		response.setCharacterEncoding("UTF-8");
		response.setHeader("Access-Control-Allow-Origin", "*");
		String getMethod = request.getMethod();
		log.debug("getMethod:" + getMethod);

		String inData = "";
		String outData = "";
		String dataSourceID = "dataSource";

		BufferedReader iny = null;
		if (getMethod.equals("GET")) {
			inData = request.getParameter("data");
			log.debug("原始Get请求参数:" + inData);
		} else {
			InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8");
			iny = new BufferedReader(reader);
			String line;
			while ((line = iny.readLine()) != null) {
				inData += line;
			}
			log.debug("原始Post请求参数:" + inData);
		}

		inData = URLDecoder.decode(inData, "UTF-8");
		log.debug("原始inData URLDecoder:" + inData);

		try {
			log.debug("开始执行...");
			
			String hsttype = "";//获取是否是手动调用 1001
			String datetime = "";//时间 20210723
			
			String json,injson="";
			//查询有多少
			def hstresult =eliteBean.chaxun_sql("select datetime as datetime,'1001' as hsttype from hst_qw_batch_his where  hst_type=2 order by  datetime asc", "dataSource");
			def count=hstresult.size();
			log.debug("count:"+count);
			if(count>0){
				for (int i = 0; i < count; i++) {
					datetime = hstresult.get(i).get("datetime");
					hsttype = hstresult.get(i).get("hsttype");
					
					json="{\"datetime\":\""+datetime+"\",\"hsttype\":\""+hsttype+"\"}";
					Map<String, String> headMap = new HashMap<String, String>();
					headMap.put("Content-Type", "application/json");
					def sysurl = "http://127.0.0.1:8080/EliteGeneralHttpTask/GeneralService/HST_API_SftpFile_Client_Tag";
					
					injson=Util.doHttpPostUseHead(sysurl,json,"UTF-8",100000,0,headMap);
					log.debug("injson:	"+injson);
				}
			}

		} catch (Exception e) {
			log.error("接口处理异常" + e.getMessage());
			// 失败返回
			outData = "{\"result\":\"-1\",\"message\":\"SSO登陆处理失败(" + e.getMessage() + ")\",\"data\":[]}";
		}
		log.debug("处理完成返回结果:" + outData);

		return outData;
	}
	
	// 解析Json值
	public static String GetJV(JSONObject inJson, String getKey) {
		String Rev_Value = null;
		try {
			Rev_Value = inJson.getString(getKey);
		} catch (Exception e) {
			try {
				Rev_Value = String.valueOf(inJson.getInt(getKey));
			} catch (Exception e1) {
				try {
					Rev_Value = String.valueOf(inJson.getJSONArray(getKey));
				} catch (Exception e2) {
					try {
						Rev_Value = String.valueOf(inJson.getJSONObject(getKey));
					} catch (Exception e3) {
						Rev_Value = "";
					}
				}
			}
		} finally {
			if (Rev_Value == null || Rev_Value.equals("null") || Rev_Value.length() <= 0) {
				Rev_Value = "";
			} else {
			}
		}
		log.debug("解析-k=" + getKey + " v=" + Rev_Value);
		return Rev_Value;
	}


	// 非空校验isNull
	public static boolean isNull(String value) {

		boolean reFlag = true;
		if (value == "" || value.length() <= 0) {
			reFlag = true;
		} else {
			reFlag = false;
		}
		log.debug(reFlag);
		return reFlag;
	}

	// 非空校验isNullMSG
	public static String isNullMSG(String key, String value) {

		String reResult = "";
		if (value == "" || value.length() <= 0) {
			reResult = key + "必填字段;";
		}
		return reResult;
	}

}
